﻿namespace RsCms.Core.Settings
{
    public class SettingsDomainService : ISettingsDomainService
    {
        ISettingRepository settingRepository;
     
       
        public SettingsDomainService(ISettingRepository settingRepository )
        {
            this.settingRepository = settingRepository;
        }

         

        public async Task<List<Entities.Settings>> SaveAsync(List<Entities.Settings> settings)
        {
            var group = settings.First().Group;
            var data = await settingRepository.Select.Where(x => x.Group == group).ToListAsync();
            if (data == null)
                data = new List<Entities.Settings>();

            List<Entities.Settings> insertData = new List<Entities.Settings>();
            List<Entities.Settings> updateData = new List<Entities.Settings>();
             
            foreach (var setting in settings)
            {
                Entities.Settings info = await settingRepository.Select.Where(x => x.Group == group && setting.Key == x.Key).ToOneAsync();
                if(info!=null)
                {
                    bool update = false;
                    if(info.Value!=setting.Value)
                    {
                        info.Value = setting.Value;
                        info.UpdatedAt = DateTime.Now; 
                        update = true;
                    }
                    if(info.Group!=setting.Group)
                    {
                        info.UpdatedAt = DateTime.Now;
                        info.Group=setting.Group; 
                        update = true;
                    }
                    if(info.Key!=setting.Key)
                    {
                        info.UpdatedAt = DateTime.Now;
                        info.Key=setting.Key;
                        update = true;
                    }
                    if(info.Order!=setting.Order)
                    {
                        info.UpdatedAt = DateTime.Now;
                        info.Order = setting.Order;
                        update = true;
                    }
                    if(update)
                    {
                         settingRepository.Update(info);
                    }
                   
                }else
                {
                    setting.Id = Guid.NewGuid().ToString("N");
                    setting.CreatedAt = DateTime.Now;
                    insertData.Add(setting);
                   
                } 
            }  
           
             if(insertData.Count>0)
            {
                settingRepository.Insert(insertData);
            }
             
            var target = await GetSettingListAsync(group);
            return target;
        }


        public async Task<List<Entities.Settings>> GetSettingListAsync(string group)
        {
            List<Entities.Settings> info = new List<Entities.Settings>();
            if (group=="site")
            {
                info= await InitSettingsAsync(group,SiteSettings());
            }else
            {
                info =await settingRepository.Select.Where(x => x.Group == group).OrderBy(x => x.Order).ToListAsync();
            }
            return info;
        }

        public async Task DeleteAsync(string id)
        {
            var info = await settingRepository.Select.Where(x => x.Id == id).ToOneAsync();
            if(info!=null)
            {
                 settingRepository.Delete(info);
            }
        }
        async Task<List<Entities.Settings>> InitSettingsAsync(string group,List<Entities.Settings> target)
        {
            var info = await settingRepository.Select.Where(x => x.Group == group).OrderBy(x=> x.Order).ToListAsync();
            bool addDababase = false;
            if (info == null)
            {
                addDababase = true;
                info = new List<Entities.Settings>();
            }
                
            foreach (var item in target)
            {
                var model = info.FirstOrDefault(x => x.Key == item.Key);
                if (model == null)
                {
                    info.Add(item);
                }
            }

            if(addDababase)
            {
                settingRepository.Insert(info);
            }

            return info;
        }

        

        List<Entities.Settings> SiteSettings()
        {
            string group = "site";
            List<Entities.Settings> settings = new List<Entities.Settings>();
            
            Entities.Settings setting = new Entities.Settings
            {
                Group = group,
                Key = "title",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=1
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "description",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=2
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "logo",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "cover_image",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "icon",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "accent_color",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "locale",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "timezone",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "codeinjection_head",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "codeinjection_foot",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "facebook",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "twitter",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "navigation",
                Type = "array",
                Value = "[]",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "secondary_navigation",
                Type = "array",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "meta_title",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=1
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "meta_description",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=2
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "og_image",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=3
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "og_title",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=1
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "og_description",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=2
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "twitter_image",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=1
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "twitter_title",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=2
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "twitter_description",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now,
                Order=3
            };
            settings.Add(setting);
            setting = new Entities.Settings
            {
                Group = group,
                Key = "open_register",
                Type = "bool",
                Value = "false",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);

            return settings;
        }

        List<Entities.Settings> CoreSettings()
        {
            string group = "core";
            List<Entities.Settings> settings = new List<Entities.Settings>();

            Entities.Settings setting = new Entities.Settings
            {
                Group = group,
                Key = "next_update_check",
                Type = "string",
                Value = "",
                CreatedAt = DateTime.Now
            };
            settings.Add(setting);

            return settings;
        }
    }
}
